/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.opensource.jdom.taggen;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom.Content;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.Parent;
import org.jdom.Text;
import org.opensource.jdom.taggen.util.AttributeUtil;
import org.opensource.jdom.taggen.util.ClassUtil;
import org.opensource.jdom.taggen.util.TagUtil;
import org.opensource.jdom.taggen.util.WriterUtil;

/**
 * This reprecent a HTML/XML tag extends any Object with this class for give the
 * Tag functionalaty. also you can use the Jdom Element functions.
 *
 * @see org.jdom.Element
 * @author sergio.valdez
 */
public class Tag extends Element {

    /**
     * Default Constructor, this whill create the default standars tag name i.e.
     * if you have a class called Div it will create a tag called &lt;div&gt;
     */
    public Tag() {
        super();
        super.setName(TagUtil.getStandarTagName(this));
        super.setNamespace(Namespace.NO_NAMESPACE);
    }

    public Tag(String name) {
        super(name, Namespace.NO_NAMESPACE);
    }

    public void getTag() {
        createAttributes();
    }

    public String write(){
        this.getTag();
        return WriterUtil.getString(this);
    }

    @Override
    public Element addContent(Content content) {
        if (!(content instanceof Text)) ((Tag)content).getTag();
        return super.addContent(content);
    }

    @Override
    public Element setContent(Collection newContent) {
        for (Object object : newContent) {
            if (!(object instanceof Text)) ((Tag)object).getTag();
        }
        return super.setContent(newContent);
    }

    @Override
    public Element addContent(Collection newContent) {
        for (Object object : newContent) {
            if (!(object instanceof Text)) ((Tag)object).getTag();
        }
        return super.addContent(newContent);
    }

    @Override
    public Element addContent(int index, Collection newContent) {
        for (Object object : newContent) {
            if (!(object instanceof Text)) ((Tag)object).getTag();
        }
        return super.addContent(index, newContent);
    }

    @Override
    public Element setContent(Content child) {
        if (!(child instanceof Text)) ((Tag)child).getTag();
        return super.setContent(child);
    }


    @Override
    public Parent setContent(int index, Collection newContent) {
        for (Object object : newContent) {
            if (!(object instanceof Text)) ((Tag)object).getTag();
        }
        return super.setContent(index, newContent);
    }

    @Override
    public Element setContent(int index, Content child) {
        if (!(child instanceof Text)) ((Tag)child).getTag();
        return super.setContent(index, child);
    }


    private void createAttributes(){
        try {
            AttributeUtil.createAttributes(this,
                    ClassUtil.methodsAsString(Tag.class));
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Tag.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(Tag.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(Tag.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
